home *** CD-ROM | disk | FTP | other *** search
- /* debug utilities
- 94/01/20 aih - updated debug window to use new document library
- 94/01/06 aih - added debug window, removed THINK C console window
- 93/12/20 aih - added memory display windoid
- 93/12/04 aih - added debugger command
- 93/03/18 aih - added console show/hide command
- 93/03/14 aih - created */
-
- #include <stdio.h>
- #include <string.h>
- #include "DebugLib.h"
- #include "MacLib.h"
- #include "ResourceConstantsLib.h"
-
- extern int _profile, _trace; /* profiler flags */
-
- static void DebugDocumentOpen(void);
-
- /*----------------------------------------------------------------------------*/
- /* debug menu */
- /*----------------------------------------------------------------------------*/
-
- static Boolean enabled(void)
- {
- return(_assert.require && _assert.ensure && _assert.check);
- }
-
- void DebugAdjustMenu(void)
- {
- if (GetMHandle(RLM_DEBUG)) {
- MenuCmdEnable(CMD_DEBUG);
- MenuCmdEnable(CMD_DEBUG_WINDOW);
- if (MacHasDebugger()) MenuCmdEnable(CMD_DEBUG_DEBUGGER);
- #if ASSERT_REQUIRE || ASSERT_ENSURE || ASSERT_CHECK
- MenuCmdEnable(CMD_DEBUG_ASSERT_ALL);
- #if ASSERT_REQUIRE
- MenuCmdEnable(CMD_DEBUG_ASSERT_REQUIRE);
- MenuCmdCheck(CMD_DEBUG_ASSERT_REQUIRE, _assert.require);
- #endif
- #if ASSERT_ENSURE
- MenuCmdEnable(CMD_DEBUG_ASSERT_ENSURE);
- MenuCmdCheck(CMD_DEBUG_ASSERT_ENSURE, _assert.ensure);
- #endif
- #if ASSERT_CHECK
- MenuCmdEnable(CMD_DEBUG_ASSERT_CHECK);
- MenuCmdCheck(CMD_DEBUG_ASSERT_CHECK, _assert.check);
- #endif
- MenuCmdEnable(CMD_DEBUG_ASSERT_BREAK);
- MenuCmdEnable(CMD_DEBUG_ASSERT_RAISE);
- MenuCmdEnable(CMD_DEBUG_ASSERT_STACK);
- MenuCmdCheck(CMD_DEBUG_ASSERT_ALL, enabled());
- MenuCmdCheck(CMD_DEBUG_ASSERT_BREAK, _assert.debug);
- MenuCmdCheck(CMD_DEBUG_ASSERT_RAISE, _assert.raise);
- MenuCmdCheck(CMD_DEBUG_ASSERT_STACK, _assert.stack);
- #endif /* ASSERT_REQUIRE || ASSERT_ENSURE || ASSERT_CHECK */
- #if ASSERT_TRACE
- MenuCmdEnable(CMD_DEBUG_ASSERT_TRACE);
- MenuCmdCheck(CMD_DEBUG_ASSERT_TRACE, _assert.trace);
- #endif
- #if PROFILE
- MenuCmdEnable(CMD_DEBUG_TRACE);
- MenuCmdEnable(CMD_DEBUG_PROFILE);
- MenuCmdCheck(CMD_DEBUG_TRACE, _trace);
- MenuCmdCheck(CMD_DEBUG_PROFILE, _profile);
- #endif
- }
- }
-
- Boolean DebugMenu(const MenuPickType *pick)
- {
- Boolean handled = true;
-
- switch (pick->cmd) {
- case CMD_DEBUG_DEBUGGER:
- DebugStr((StringPtr) "\p Test Application");
- break;
- case CMD_DEBUG_WINDOW:
- DebugDocumentOpen();
- break;
- case CMD_DEBUG_ASSERT_ALL:
- _assert.require = _assert.ensure = _assert.check = ! enabled();
- break;
- case CMD_DEBUG_ASSERT_REQUIRE:
- _assert.require = ! _assert.require;
- break;
- case CMD_DEBUG_ASSERT_ENSURE:
- _assert.ensure = ! _assert.ensure;
- break;
- case CMD_DEBUG_ASSERT_CHECK:
- _assert.check = ! _assert.check;
- break;
- case CMD_DEBUG_ASSERT_BREAK:
- _assert.debug = ! _assert.debug;
- break;
- case CMD_DEBUG_ASSERT_RAISE:
- _assert.raise = ! _assert.raise;
- break;
- case CMD_DEBUG_ASSERT_TRACE:
- _assert.trace = ! _assert.trace;
- break;
- case CMD_DEBUG_ASSERT_STACK:
- _assert.stack = ! _assert.stack;
- break;
- case CMD_DEBUG_TRACE:
- _trace = ! _trace;
- break;
- case CMD_DEBUG_PROFILE:
- _profile = ! _profile;
- break;
- default:
- handled = false;
- break;
- }
- return(handled);
- }
-
- /*----------------------------------------------------------------------------*/
- /* debug window */
- /*----------------------------------------------------------------------------*/
-
- #include "ApplicationLib.h"
- #include "ApplicationPreferencesLib.h"
- #include "PreferencesLib.h"
- #include "ResourceLib.h"
- #include "TextDocumentLib.h"
- #include "WindowLib.h"
-
- static DocumentHandle gDebugDocument; /* document for displaying debug messages */
-
- /* handler called by document library to close the debug window */
- static void DebugDocumentEnd(DocumentHandle doc)
- {
- require(doc == gDebugDocument);
- TxDocEnd(gDebugDocument);
- gDebugDocument = NULL;
- ensure(! gDebugDocument);
- }
-
- /* open the debug window */
- static void DebugDocumentOpen(void)
- {
- FileNameType name; /* name of debug log file */
- FileType file, *fp = &file; /* debug log file */
- TextPrefsType *prefs = NULL; /* for setting prefs for new document */
- TextPrefsType svprefs; /* for saving original prefs settings */
- volatile DocumentHandle doc = NULL; /* for creating the debug document */
- static DocumentTableType table;/* for overriding document's end handler */
-
- TRY {
- if (! gDebugDocument) {
-
- /* create debug file if it doesn't already exist */
- ResStrLen(RLS_FILE, RLS_FILE_DEBUG, name, sizeof(FileNameType));
- FileSetPref(fp, name);
- if (! FileExists(fp))
- FileCreate(fp, AppCreator(), 'TEXT');
-
- /* change preferences so document will use the fastest version of
- a text document (an unstyled document without word wrap) */
- prefs = &AppPrefs()->text;
- svprefs = *prefs;
- prefs->kind = TX_UNSTYLED_KIND; /* program_note: due to a bug in TE32K can't use TX_EXTENDED_KIND */
- prefs->wrap = false;
-
- /* Create the document, setting the autosave flag so it will
- automatically be saved when closed or when we quit. */
- doc = DocBegin(fp);
- DocAutoSaveSet(doc, true);
-
- /* Override the end handler so that we can clear the gDebugDocument
- global when the document is closed. */
- table = *DocTable(doc);
- table.end = DebugDocumentEnd;
- DocTableSet(doc, &table);
- gDebugDocument = doc;
-
- WinShow(DocWindow(doc));
- }
- WinSelect(DocWindow(gDebugDocument));
- } CLEANUP {
- if (prefs)
- *prefs = svprefs;
- } CATCH {
- DocEnd(doc);
- } ENDTRY;
- ensure(WinFirstVisible(WinLayer(DocWindow(gDebugDocument))) ==
- DocWindow(gDebugDocument));
- }
-
- /* print the string to the debug window */
- void DebugVPrintf(const char *fmt, va_list ap)
- {
- TextScrollHandle scrl;
- TextHandle text;
- char out[512];
- int i, length;
-
- length = vsprintf(out, fmt, ap);
- check(0 <= length && length < sizeof(out));
- for (i = 0; i < length; i++) {
- if (out[i] == '\n')
- out[i] = '\r';
- }
- if (! gDebugDocument)
- DebugDocumentOpen();
- scrl = TxDocTextScroll(gDebugDocument);
- text = TxScrlText(scrl);
- TxSelEndSet(text, TxLength(text));
- TxSelStartSet(text, TxLength(text));
- TxInsert(text, out, length, NULL);
- TxScrlChanged(scrl);
- }
-
- void DebugPrintf(const char *fmt, ...)
- {
- va_list ap;
-
- va_start(ap, fmt);
- DebugVPrintf(fmt, ap);
- va_end(ap);
- }
-